package net.lab1024.sa.admin.module.business.erp.bas.service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.bas.dao.BomordDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.BomordEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.BomordAddForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.BomordQueryForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.BomordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.vo.BomordVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * BOM表 Service
 *
 * @Author AKITA
 * @Date 2025-08-11 22:28:03
 * @Copyright AKITA
 */

@Service
public class BomordService {

    @Resource
    private BomordDao bomordDao;

    /**
     * 分页查询
     */
    public PageResult<BomordVO> queryPage(BomordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<BomordVO> list = bomordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(BomordAddForm addForm) {
        BomordEntity bomordEntity = SmartBeanUtil.copy(addForm, BomordEntity.class);
        bomordDao.insert(bomordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(BomordUpdateForm updateForm) {
        BomordEntity bomordEntity = SmartBeanUtil.copy(updateForm, BomordEntity.class);
        bomordDao.updateById(bomordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        bomordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    public ResponseDTO<String> delete(Integer bomId) {


        bomordDao.deleteById(bomId);
        return ResponseDTO.ok();
    }

    public ResponseDTO<List<BomordEntity>> info(Long id) {
        ArrayList<BomordEntity> bomordEntities = new ArrayList<>();
        BomordEntity bomordEntity = bomordDao.selectById(id);
        tree(bomordEntity);
        bomordEntities.add(bomordEntity);
        return ResponseDTO.ok(bomordEntities);
    }

    private void tree(BomordEntity bomordEntity) {
        List<BomordEntity> bomordEntities = bomordDao.selectList(new LambdaQueryWrapper<BomordEntity>().eq(BomordEntity::getParentId, bomordEntity.getId()));
        bomordEntity.setChildren(bomordEntities);
        for (BomordEntity entity : bomordEntities) {
            tree(entity);
        }
    }
}
